package com.mabs.dao.impl;

import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

import com.mabs.dao.PersonDao;
import com.mabs.dao.PersonSQL;
import com.mabs.entity.Gender;
import com.mabs.entity.Person;
import com.mabs.util.JDBCTemplate;

public class PersonDaoImpl implements PersonDao, PersonSQL {

	@Override
	public List<Person> selectAllPersons() {
		List<Person> persons = new ArrayList<>();
		JDBCTemplate.query(conn -> conn.prepareStatement(SELECT_ALL_PERSONS), rs -> {
			Person person = null;
			while (rs.next()) {
				Long id = rs.getLong("id");
				String name = rs.getString("name");
				Gender gender = Gender.valueOf(rs.getString("gender").equals("男") ? "MALE" : "FEMALE");
				String phone = rs.getString("phone");
				String qq = rs.getString("qq");
				String address = rs.getString("address");
				person = new Person(id, name, gender, phone, qq, address);
				persons.add(person);
			}
			return null;
		});
		return persons;
	}

	@Override
	public Person selectPersonById(Long id) {
		return (Person) JDBCTemplate.query(conn -> conn.prepareStatement(SELECT_PERSON_BY_ID + id), rs -> {
			Person person = null;
			if (rs.next()) {
				String name = rs.getString("name");
				Gender gender = Gender.valueOf(rs.getString("gender").equals("男") ? "MALE" : "FEMALE");
				String phone = rs.getString("phone");
				String qq = rs.getString("qq");
				String address = rs.getString("address");
				person = new Person(id, name, gender, phone, qq, address);
			}
			return person;
		});
	}

	@Override
	public List<Person> selectPersonByName(String name) {
		List<Person> persons = new ArrayList<>();
		JDBCTemplate.query(conn -> conn.prepareStatement(SELECT_PERSON_BY_NAME + "'%" + name + "%'"), rs -> {
			Person person = null;
			while (rs.next()) {
				Long id = rs.getLong("id");
				String name1 = rs.getString("name");
				Gender gender = Gender.valueOf(rs.getString("gender").equals("男") ? "MALE" : "FEMALE");
				String phone = rs.getString("phone");
				String qq = rs.getString("qq");
				String address = rs.getString("address");
				person = new Person(id, name1, gender, phone, qq, address);
				persons.add(person);
			}
			return null;
		});
		return persons;
	}

	@Override
	public int insertPerson(Person person) {
		return JDBCTemplate.update(conn -> {
			PreparedStatement pst = conn.prepareStatement(INSERT_PERSON);
			pst.setString(1, person.getName());
			pst.setString(2, person.getGender().toString());
			pst.setString(3, person.getPhone());
			pst.setString(4, person.getQq());
			pst.setString(5, person.getAddress());
			return pst;
		});
	}

	@Override
	public int deletePersonById(Long id) {
		return JDBCTemplate.update(conn -> conn.prepareStatement(DELETE_PERSON_BY_ID + id));
	}

	@Override
	public int updatePerson(Person person) {
		return JDBCTemplate.update(conn -> {
			Long id = person.getId();
			String name = person.getName();
			String gender = person.getGender().toString();
			String phone = person.getPhone();
			String qq = person.getQq();
			String address = person.getAddress();
			String sql = "update jdbc_person set id=" + id;
			if (id != null) {
				if (name != null) {
					sql += ",name='" + name + "'";
				}
				if (gender != null) {
					sql += ",gender='" + gender + "'";
				}
				if (phone != null) {
					sql += ",phone='" + phone + "'";
				}
				if (qq != null) {
					sql += ",qq='" + qq + "'";
				}
				if (address != null) {
					sql += ",address='" + address + "'";
				}
			}
			sql += " where id=" + id;
			PreparedStatement pst = conn.prepareStatement(sql);
			return pst;
		});
	}

	@Override
	public Person selectPersonByPhone(String phone) {
		return (Person) JDBCTemplate.query(conn -> conn.prepareStatement(SELECT_PERSON_BY_PHONE + phone), rs -> {
			Person person = null;
			if (rs.next()) {
				Long id = rs.getLong("id");
				String name = rs.getString("name");
				Gender gender = Gender.valueOf(rs.getString("gender").equals("男") ? "MALE" : "FEMALE");
				String qq = rs.getString("qq");
				String address = rs.getString("address");
				person = new Person(id, name, gender, phone, qq, address);
			}
			return person;
		});
	}

}
